package cn.com.ddapp.ddm.fragment.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.ui.dialog.DialogControl;
import cn.com.ddapp.ddm.ui.dialog.WaitDialog;

/**
 * Created by idea on 2016/6/25.
 */
public abstract class BaseFragment extends Fragment {

	protected Gson gson = new Gson();

	Unbinder unbinder;

	public static final int STATE_NONE = 0;
	public static final int STATE_REFRESH = 1;
	public static final int STATE_LOADMORE = 2;
	public static final int STATE_NOMORE = 3;
	public static final int STATE_PRESSNONE = 4;// 正在下拉但还没有到刷新的状态
	public static final int STATE_NOLOGIN = 5;//未登录
	public static int mState = STATE_NONE;
	protected FragmentActivity activity;

	@Override
	public void onAttach( Activity activity) {
		Log.e(getFragmentTag(), "onAttach: ");
		super.onAttach(activity);
		this.activity = (FragmentActivity) activity;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		Log.e(getFragmentTag(), "onCreate: ");
		super.onCreate(savedInstanceState);
	}

	protected abstract String getFragmentTag();

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		Log.e(getFragmentTag(), "onCreateView: ");
		View view = inflater.inflate(getLayoutId(), container, false);
		unbinder = ButterKnife.bind(this, view);
		initView(view, savedInstanceState);
		initData();
		return view;
	}
	protected abstract void initData();
	protected abstract void initView(View view, Bundle savedInstanceState);

	//获取fragment布局文件ID
	protected abstract int getLayoutId();

	@Override
	public void onDestroyView() {
		Log.e(getFragmentTag(), "onDestroyView: ");
		super.onDestroyView();
	}

	@Override
	public void onDetach() {
		Log.e(getFragmentTag(), "onDetach: ");
		super.onDetach();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		unbinder.unbind();
	}

	protected void hideWaitDialog() {
		FragmentActivity activity = getActivity();
		if (activity instanceof DialogControl) {
			((DialogControl) activity).hideWaitDialog();
		}
	}

	protected WaitDialog showWaitDialog() {
		return showWaitDialog(R.string.loading);
	}

	protected WaitDialog showWaitDialog(int resid) {
		FragmentActivity activity = getActivity();
		if (activity instanceof DialogControl) {
			return ((DialogControl) activity).showWaitDialog(resid);
		}else{
			return null;
		}

	}

	protected WaitDialog showWaitDialog(String resid) {
		FragmentActivity activity = getActivity();
		if (activity instanceof DialogControl) {
			return ((DialogControl) activity).showWaitDialog(resid);
		}
		return null;
	}

	protected void showNext(Fragment fragment, int id) {
		showNext(fragment, id, null, true);
	}
	protected void showNext(Fragment fragment, int id,Bundle b) {
		showNext(fragment, id, b, true);
	}


	protected void showNext(Fragment fragment, int id, Bundle b,boolean isAddBackStack) {
		FragmentManager fragmentManager = activity.getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
		fragmentTransaction.replace(id, fragment);
		if (b != null) {
			fragment.setArguments(b);
		}
		if(isAddBackStack){
			fragmentTransaction.addToBackStack(null);
		}
		fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		fragmentTransaction.commitAllowingStateLoss();
	}

	/**
	 * 返回上一个界面
	 */
	protected void back() {
//		showHideFragment();
		getActivity().getSupportFragmentManager().popBackStackImmediate();
	}
}
